Ext.define('HIT.controller.MdataGoods', {
    extend: 'Ext.app.Controller',

    models: ['MdataGoods'],
    stores: ['MdataGoods'],
    views: ['MdataGoodsPanel', 'MdataGoodsAddPanel', 'MdataGoodsEditPanel'],
    onButtonClick: function(button, e, options) {
        //    	var p;
        //        var grid=Ext.getCmp('tab-madta-goods');        
        //        if (grid == undefined) {
        //			return;
        //		}
        if (button.id == 'mdata-goods-add') {
            add = Share.createPanelByAlias('widget.mdatagoodsaddpanel');
            Share.showPanelAsWindow(add);
        } else if (button.id == 'mdata-goods-edit') {  //修改	
        	var check = Share.checkFun();
			if (check) {
	        	var tabpanelId = 'tab-mdata-goods';
	            var tab = Ext.getCmp(tabpanelId);
	            if (tab == undefined) {
	                return;
	            }
	
	            if (tab.getSelectionModel().getCount() == 0) {
	                Share.ShowMessage("I0011", "一条记录");
	                return;
	            }
	            if (tab.getSelectionModel().getCount() != 1) {
	                Share.ShowMessage("E0039", "记录");
	                return;
	            }
	
	            var row = tab.getSelectionModel().getSelection()[0];
	            edit = Share.createPanelByAlias('widget.mdatagoodseditpanel');
	            Ext.getCmp('hiddenID').setValue(row.get('id'));
	            var fix = Ext.getCmp('tab-mdata-goods').getSelectionModel().getSelection()[0];
	            Ext.getCmp('mdata-goods-code-edit').setValue(fix.get('code'));
	            Ext.getCmp('mdata-goods-name-edit').setValue(fix.get('name'));
	            Ext.getCmp('mdata-goods-item-edit').setValue(fix.get('tax_item'));
	            Ext.getCmp('mdata-goods-rate-edit').setValue(fix.get('tax_rate'));
	            Ext.getCmp('mdata-goods-speci-edit').setValue(fix.get('specification'));
	            Ext.getCmp('mdata-goods-meas-edit').setValue(fix.get('measurement'));
	            Ext.getCmp('mdata-goods-price-edit').setValue(fix.get('default_price'));
	            Ext.getCmp('mdata-goods-shortcode-edit').setValue(fix.get('shortcode'));
	            Share.showPanelAsWindow(edit);
			}else{
				Share.ShowMessage("E0026");
				return;
			}
        } else if (button.id == 'mdata-goods-delete') {  //删除	
            var tabpanelId = 'tab-mdata-goods';
            var tab = Ext.getCmp(tabpanelId);
            if (tab == undefined) {
                return;
            }

            if (tab.getSelectionModel().getCount() == 0) {
                Share.ShowMessage("I0008");
                return;
            }
            //            if (tab.getSelectionModel().getCount() != 1) {
            //                Ext.MessageBox.alert(Share.T('一次只能选择一条记录!'));
            //                return;
            //            }

            var row = tab.getSelectionModel().getSelection();

            var Id_Arr = "(";
            for (var ri_d = 0; ri_d < row.length; ri_d++) {
                Id_Arr += "'" + row[ri_d].get('id') + "'";
                if (ri_d < row.length - 1) {
                    Id_Arr += ",";
                }
            }
            Id_Arr += ")";

            Ext.MessageBox.confirm(Share.T('提示'), Share.T('确定删除吗？'), function(btn) {
                if (btn == 'yes') {
                	var check = Share.checkFun();
        			if (check) {
	                    Share.AjaxRequest({
	                        url: ctx + "/deletemdatagoods",
	                        params: {
	                            id: Id_Arr
	                        },
	                        showMsg: false,
	                        scope: this,
	                        callback: function(json) {
	                            /*if (Ext.getCmp('tab-mdata-goods').getStore().getCount() == 1) {
	                                Ext.getCmp('tab-mdata-goods').getStore().removeAll();
	                                Ext.getCmp('mdata-goods-pag').movePrevious();
	                            }*/
	                        	tab.getStore().reload();
	                        	//修改删除数据刷新数量
    							var toolbar = Ext.getCmp('mdata-goods-pag');
                                var fromRecord = toolbar.getPageData().fromRecord;
                                var toRecord = toolbar.getPageData().toRecord;
                                if((toRecord-fromRecord+1)==row.length){
                                	toolbar.movePrevious();
                                }
	                            Share.ShowMessage("I0006","删除"); 
	                        },
	                        falseFun: function(json) { //失败后想做的个性化操作函数
	                            Ext.MessageBox.alert(json.msg);
	                        }
	                    });
        			}else {
        				Share.ShowMessage("E0026");
        				return;
        			}
                }
            });
        }
    },
    save: function(win) {  //保存(添加/修改)
        var tabpanelId = 'tab-mdata-goods';
        var tab = Ext.getCmp(tabpanelId);
        if (tab == undefined) {
            return;
        }

        var functionId = Ext.getCmp('mdata-functionId').value;

        if (functionId == 1) {  //添加-保存
            //        	Ext.MessageBox.alert("AAA");
            var goodscode = Ext.getCmp('mdata-goods-code-add').getValue();
            var goodsname = Ext.getCmp('mdata-goods-name-add').getValue();
            var goodsitem = Ext.getCmp('mdata-goods-item-add').getValue();
            var goodsrate = Ext.getCmp('mdata-goods-rate-add').getValue();
            var goodsspeci = Ext.getCmp('mdata-goods-speci-add').getValue();
            var goodsmeas = Ext.getCmp('mdata-goods-meas-add').getValue();
            var goodsprice = Ext.getCmp('mdata-goods-price-add').getValue();
            var goodsshort = Ext.getCmp('mdata-goods-shortcode-add').getValue();
            if (!Ext.getCmp('mdata-goods-code-add').isValid()) {
				return;
			}
            
            if (!Ext.getCmp('mdata-goods-name-add').isValid()) {
				return;
			}
            
            var GoodsStore = Ext.getCmp('tab-mdata-goods').getStore();
            for (var i = 0; i < GoodsStore.getCount(); i++) {
                if (goodscode == GoodsStore.getAt(i).get('code')) {
                    Share.ShowMessage("I0013", "相同的代码");
                    return;
                }
            }
            Share.AjaxRequest({
                url: ctx + "/addmdatagoods",
                params: {
                    goodscode: goodscode,
                    goodsname: goodsname,
                    goodsitem: goodsitem,
                    goodsrate: goodsrate,
                    goodsspeci: goodsspeci,
                    goodsmeas: goodsmeas,
                    goodsprice: goodsprice,
                    goodsshort: goodsshort
                },
                showMsg: false,
                callback: function(json) {
                	Share.ShowMessage("I0006","添加");
                    tab.getStore().reload();
                    win.close();
                },
                falseFun: function(json) { //失败后想做的个性化操作函数
                    //                	 Ext.MessageBox.alert(json.msg);	
                }
            });
        } else if (functionId == 2) {  //修改-保存
            var goodscode = Ext.getCmp('mdata-goods-code-edit').getValue();
            var goodsname = Ext.getCmp('mdata-goods-name-edit').getValue();
            var goodsitem = Ext.getCmp('mdata-goods-item-edit').getValue();
            var goodsrate = Ext.getCmp('mdata-goods-rate-edit').getValue();
            var goodsspeci = Ext.getCmp('mdata-goods-speci-edit').getValue();
            var goodsmeas = Ext.getCmp('mdata-goods-meas-edit').getValue();
            var goodsprice = Ext.getCmp('mdata-goods-price-edit').getValue();
            var goodsshort = Ext.getCmp('mdata-goods-shortcode-edit').getValue();
            var id = Ext.getCmp('hiddenID').value;
            
            if (!Ext.getCmp('mdata-goods-code-edit').isValid()) {
				return;
			}
            if (!Ext.getCmp('mdata-goods-name-edit').isValid()) {
				return;
			}
            
            Share.AjaxRequest({
                url: ctx + "/updatemdatagoods",
                params: {
                    goodscode: goodscode,
                    goodsname: goodsname,
                    goodsitem: goodsitem,
                    goodsrate: goodsrate,
                    goodsspeci: goodsspeci,
                    goodsmeas: goodsmeas,
                    goodsprice: goodsprice,
                    goodsshort: goodsshort,
                    id: id
                },
                showMsg: false,
                callback: function(json) {
                	Share.ShowMessage("I0006","修改");
                    tab.getStore().reload();
                    win.close();
                },
                falseFun: function(json) { //失败后想做的个性化操作函数
                    Ext.MessageBox.alert(json.msg);
                }
            });
        }
    },

    init: function(application) {
        this.control({
            "button": {
                click: this.onButtonClick
            }
        });
    }
});